package com.violet.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    // 1.用户确认收货的交换机
    public static final String EXCHANGE_COMPLETED = "supermarket.exchange.completed";
    // 对应的 Queue 和 Routing Key
    public static final String QUEUE_COMPLETED = "supermarket.queue.completed";
    public static final String ROUTING_KEY_COMPLETED = "supermarket.routing.completed";

    // 2.用户进行支付了的交换机
    public static final String EXCHANGE_PAID = "supermarket.exchange.paid";
    // 对应的 Queue 和 Routing Key
    public static final String QUEUE_PAID = "supermarket.queue.paid";
    public static final String ROUTING_KEY_PAID = "supermarket.routing.paid";

    // 声明交换机
    @Bean
    public Exchange exchange1() {
        return ExchangeBuilder
            .directExchange(EXCHANGE_COMPLETED)
            .durable(true)
            .build();
    }

    @Bean
    public Exchange exchange2() {
        return ExchangeBuilder
            .directExchange(EXCHANGE_PAID)
            .durable(true)
            .build();
    }

    // 声明队列
    @Bean
    public Queue queue1() {
        return new Queue(QUEUE_COMPLETED);
    }

    @Bean
    public Queue queue2() {
        return new Queue(QUEUE_PAID);
    }


    // 队列绑定到交换机
    @Bean
    public Binding binding1() {
        return BindingBuilder.bind(queue1()).to(exchange1()).with(ROUTING_KEY_COMPLETED).noargs();
    }

    @Bean
    public Binding binding2() {
        return BindingBuilder.bind(queue2()).to(exchange2()).with(ROUTING_KEY_PAID).noargs();
    }

}
